/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import static persistencia.DBManagerIngresos.conexion;
import modelo.tabla.Ingresos;
import modelo.MetodosIngresos;


/**
 * aprobecharemos el codigo de Creacion y añadiremos de esta un combo box 
 * para elegir el modelo antiguo que le pasaremos al update como campo where
 * @author Kevin
 */
public class JPModificarIngreso extends javax.swing.JPanel {
     ArrayList<Ingresos> al;
    /**
     * Creates new form JPModeloCreacion
     */

            public void actualizarIngresos() throws SQLException, ClassNotFoundException{
        jComboIdAntiguo.removeAllItems();
        MetodosIngresos mti = new MetodosIngresos();
        al = mti.getIngresos();
        for (int i = 0; i < al.size(); i++) {
            jComboIdAntiguo.addItem(al.get(i).getId()); //+al.get(i).getNombre());
        }
    }
    public JPModificarIngreso() throws SQLException, ClassNotFoundException {
        initComponents();
        actualizarIngresos();

        
            jbModificar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nombreIdAntiguo = (String) jComboIdAntiguo.getSelectedItem();
                Ingresos nId = new Ingresos(nombreIdAntiguo);
                String nombreActualizado = jtfFechaA.getText();
                Ingresos nuevaMar = new Ingresos(nombreActualizado);
                MetodosIngresos mti = null;
                try {
                    mti = new MetodosIngresos();
                    mti.modIngresos(nId,nuevaMar);
                    System.out.println ("Ingreso Modificado");
                    actualizarIngresos();
                    JOptionPane.showMessageDialog(null, "Ingreso Modificado");
                } catch (ClassNotFoundException ex){
                    System.err.println ("DRIVER NO ENCONTRADO.");
                    ex.printStackTrace();
                } catch (SQLException ex) {
                    System.err.println ("SQLEXCEPTION.");
                    JOptionPane.showMessageDialog(null, "Error al Modificar el Ingreso");
                    ex.printStackTrace();
                } 
               }
        });
        
    }
   

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlNombreModelo = new javax.swing.JLabel();
        jtfFechaA = new javax.swing.JTextField();
        jbModificar = new javax.swing.JButton();
        jComboIdAntiguo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();

        jlNombreModelo.setText("Fecha de Alta");

        jtfFechaA.setMinimumSize(new java.awt.Dimension(300, 20));
        jtfFechaA.setName(""); // NOI18N
        jtfFechaA.setPreferredSize(new java.awt.Dimension(300, 20));

        jbModificar.setText("Modificar");

        jComboIdAntiguo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("IdPaciente");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlNombreModelo)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboIdAntiguo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfFechaA, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addComponent(jbModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboIdAntiguo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlNombreModelo)
                    .addComponent(jtfFechaA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(jbModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboIdAntiguo;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jbModificar;
    private javax.swing.JLabel jlNombreModelo;
    private javax.swing.JTextField jtfFechaA;
    // End of variables declaration//GEN-END:variables
}
